package ${packageName}.controller;

import ${packageName}.entity.*;
import ${packageName}.repository.*;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.ResponseEntity;

import java.util.*;

/**
* ${classInfo.classComment}
* @author ${authorName} ${.now?string('yyyy-MM-dd')}
*/
@RestController
@RequestMapping("/${classInfo.className?lower_case}")
public class ${classInfo.className}Controller {

    @Autowired
    private ${classInfo.className}Repository ${classInfo.className?uncap_first}Repository;

    /**
    * 新增或编辑
    */
    @PostMapping("/save")
    public ResponseEntity save(${classInfo.className} ${classInfo.className?uncap_first}){
        ${classInfo.className?uncap_first}Repository.save(${classInfo.className?uncap_first});
        return ResponseEntity.ok().build();
    }

    /**
    * 删除
    */
    @GetMapping("/deleteById/{id}")
    public ResponseEntity deleteById(@PathVariable ${classInfo.fieldList[0].fieldClass} id){
        ${classInfo.className?uncap_first}Repository.deleteById(id);

        return ResponseEntity.ok().build();
    }

    /**
    * 查询
    */
    @GetMapping("/findById/{id}")
    public ResponseEntity findById(@PathVariable ${classInfo.fieldList[0].fieldClass} id){
        Optional<${classInfo.className}> ${classInfo.className?uncap_first}=${classInfo.className?uncap_first}Repository.findById(id);
        if(${classInfo.className?uncap_first}.isPresent()){
            return ResponseEntity.ok(${classInfo.className?uncap_first}.get());
        }else{
            return ResponseEntity.ok().build();
        }
    }

    /**
    * 分页查询
    */
    @PostMapping("/find")
    public ResponseEntity find(${classInfo.className} ${classInfo.className?uncap_first},
                        @RequestParam(required = false, defaultValue = "0") int pageNumber,
                        @RequestParam(required = false, defaultValue = "10") int pageSize) {

            //创建匹配器，需要查询条件请修改此处代码
            ExampleMatcher matcher = ExampleMatcher.matchingAll();

            //创建实例
            Example<${classInfo.className}> example = Example.of(${classInfo.className?uncap_first}, matcher);
            //分页构造
            Pageable pageable = PageRequest.of(pageNumber,pageSize);

            return ResponseEntity.ok(${classInfo.className?uncap_first}Repository.findAll(example, pageable));
    }

}
